<!-- subpkg_archive/list/index.vue -->
<script lang="ts" setup>
import { deletePatientApi, getPatientListApi } from '@/api/patien';
import type { PatientItem } from '@/types/patien';
import { onShow } from '@dcloudio/uni-app';
import { ref } from 'vue'
defineOptions({
  options: {
    styleIsolation: "shared",
  },
});
const swipeOptions = ref([
  {
    text: '删除',
    style: {
      backgroundColor: '#dd524d',
      color: '',
      fontSize: '',
    },
  },
])

onShow(() => {
  getPatientList()
})
//获取患者数据
// 1. 患者列表
const patientList = ref<PatientItem[]>([])
// 家庭档案（患者）列表
let getPatientList = async () => {
  const data = await getPatientListApi()
  patientList.value = data
}


//1.定义删除方法
const onSwipeActionClick = async (id: string, index: number) => {
  // 调用删除患者接口
  let res = await deletePatientApi(id)
  // Vue 实例中的数据也要同步删除
  patientList.value.splice(index, 1)
  uni.showToast({
    title: '删除成功',
    icon: 'success'
  })
}
</script>

<template>
  <scroll-page>
    <view class="archive-page">
      <view class="archive-tips">最多可添加6人</view>

      <uni-swipe-action>
        <uni-swipe-action-item :right-options="swipeOptions" v-for="(item, index) in patientList" :key="item.id"
          @click="onSwipeActionClick(item.id as string, index)">
          <view class="archive-card active">
            <view class="archive-info">
              <text class="name">{{ item.name }}</text>
              <text class="id-card">{{ item.idCard.replace(/(\d{6})\d{8}(\d{4})/, '$1********$2') }}</text>
              <text class="default" v-if="item.defaultFlag">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ item.gender === 1 ? '男' : '女' }}</text>
              <text class="age">{{ item.age }}岁</text>
            </view>
            <navigator hover-class="none" class="edit-link" :url="`/subpkg_archive/form/form?id=${item.id}`">
              <uni-icons type="calendar" size="20" color="#16C2A3" custom-prefix="iconfont" />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="true" class="archive-card">
        <navigator class="add-link" hover-class="none" url="/subpkg_archive/form/form">
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>
    </view>
  </scroll-page>
</template>

<style lang="scss">
@import './list.scss';
</style>